/**
 * create by zqk on 2018/4/23.
 */

var iScheduleType = 2;
var userList;
var defaultDate;
var postData;
var uname;
var justself;
var userName ='我的';
var iUserId;
var eventdata;
var leftWidth;
$.fn.pagination.defaults.displayMsg = '';
$(document).ready(function(){
    defaultDate = new Date().format("yyyy-MM-dd");
    $.ajax({
        url:moduleUrl+'schedule/getScheduleUserNum',
        type:'POST',
        success:function (data) {
            if(data.num>0){
                leftWidth = 360;
                $('#userIds').attr('style','width: 320px;height:100%;position: absolute;display: block');
                $('#calendarDiv').attr('style','position: absolute;left: 320px;');

                userList = $('#ScheduleUser');
                userList.datagrid({
                    url:moduleUrl + 'Schedule/getScheduleUser',
                    method: 'post',
                    singleSelect:true,
                    rownumbers: true,
                    pagination: true,
                    pageSize: 50,
                    pageList: [20,50,100,200],
                    idField: 'iUserId',
                    columns:[[
                        {field:'sStaffName',title:'姓名',width:110,align:'left'},
                        {field:'sDepName',title:'部门',width:150,align:'left'},
                        // {field:'sPostName',title:'岗位',width:120,align:'left'},
                    ]],
                    onClickRow:function(rowIndex,rowData){
                        // console.log(rowData);
                        justself = 0;
                        userName = rowData.sStaffName;
                        iUserId = rowData.iUserId;
                        getSchedule()
                    },

                    onLoadSuccess:function (data) {
                        getSchedule()
                    }
                });
            }else{
                leftWidth = 70;
                $('#calendarDiv').attr('style','position: absolute;left: 0;');
                getSchedule()
            }
        },
        error:function (data) {
            window.top.xhTop.showError(
                data.responseJSON.error,
                data.responseJSON.msg
            );
        }
    });


    // $('#selfbuilt').click(function() {
    //     iScheduleType = 1;
    //     justself = 0;
    //     $(this).addClass('btn-primary').removeClass('btn-default');
    //     $('#distribution').addClass('btn-default').removeClass('btn-primary');
    //     getSchedule();
    // });
    // $('#distribution').click(function() {
    //     iScheduleType = 2;
    //     justself = 0;
    //     $(this).addClass('btn-primary').removeClass('btn-default');
    //     $('#selfbuilt').addClass('btn-default').removeClass('btn-primary');
    //     getSchedule();
    // });

    $('#justself').click(function() {
        justself = 1;
        iUserId = 0;
        userName = '我的';
        getSchedule();

    });

    $('#bt_list_search').click(function(){
        uname=$('#userListSearch').val();
        userList.datagrid('clearSelections');
        userList.datagrid('load',{uname:uname});
        justself = 0;
        userName = '';
        getSchedule()
    });
});

function getSchedule(){
    postData = {iUserId:iUserId,iScheduleType:iScheduleType,uname:uname,justself:justself};
    $.post(moduleUrl + 'Schedule/getList', postData, function(result){
        $('#calendar').fullCalendar( 'destroy');
        fullCalendarData(result);
    }, 'json')
    return false;
}

function fullCalendarData(listData) {
    $('#calendar').width(window.innerWidth-leftWidth);
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next,today',
            center: 'title',
            right: 'listDay,listWeek,month,listMonth'
        },
        views: {
            listDay: { buttonText: '天' },
            listWeek: { buttonText: '周' }
        },
        // height:'100%',
        height : window.innerHeight-26,
        windowResize: function(view) {
            $('#calendar').fullCalendar('option', 'height', window.innerHeight-26);
        },
        defaultView: 'listWeek',
        navLinks: true, // can click day/week names to navigate views
        editable: true,
        locale:'zh-cn',
        defaultDate: defaultDate,
        eventLimit: true, // allow "more" link when too many events
        dayClick: function(date, allDay, jsEvent, view) {
            var url = moduleUrl + 'Schedule/addScheduleForm?iUserId='+iUserId;
            window.top.xhTop.showModal({
                title: '添加日程',
                width:480,
                content: '<iframe frameborder="0" style="width: 100%;height:400px;" src="' + url + '"></iframe>',

            });
        },
        eventClick: function(event, jsEvent, view) {
            eventdata = event;
            var url = moduleUrl + 'Schedule/addScheduleForm?iScheduleId='+event.id;
            window.top.xhTop.showModal({
                title: '修改日程',
                width:480,
                content: '<iframe frameborder="0" style="width: 100%;height:445px;" src="' + url + '"></iframe>',
            });
        },
        events:listData
    });
    var str = '<h3 id="scheduleTitle" style="text-align:center;margin-top: 0px;font-size: 18px">'+userName+'日程  '+' </h3>';
    $('.fc-center').prepend(str);

}


function addSchedule(events){
    $('#calendar').fullCalendar('renderEvent', events, true);
}

function editSchedule(events){
    eventdata.title = events.title;
    eventdata.start = events.start;
    eventdata.end = events.end;
    eventdata.eventColor = events.color;
    eventdata.color = events.color;
    eventdata.id = events.id;
    $('#calendar').fullCalendar('updateEvent',eventdata);
}

function removeSchedule(id){
    $('#calendar').fullCalendar('removeEvents',id);
}


